﻿import HostJsResultDialog from './HostJsResultDialog.vue'
import HostJsEditDialog from './HostJsEditDialog.vue'
export default {
  components: {
    HostJsResultDialog, HostJsEditDialog
  },
  data () {
    return {
      loading: false,
      // 上部的检索列表
      searchTabList: [
        { name: '源码列表', pathValue: '/hj/source/list' },
        { name: '公用库列表', pathValue: '/hj/commlib/list' },
        { name: '已编译列表', pathValue: '/hj/compiled/list' }
      ],
      currentTabPath: '/hj/source/list',
      // 右侧的表格
      tableData: [],
      tableColumn: [
        {
          text: '脚本名称',
          indexName: 'name'
        }
      ],
      // 打开运行结果的弹窗
      resultDialog: {
        showDialog: false,
        resultValue: null
      },
      // 打开编辑的弹窗
      editDialog: {
        showDialog: false,
        resultValue: null,
        hostName: '',
        showCompiled: false
      },
      // 新增脚本时
      dialogFormVisible_add: false,
      ruleForm: {
        addName: ''
      },
      // 表单规则部分
      rules: {
        addName: [
          {required: true, message: '脚本名称不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  created () {
    this.getAllData()
  },
  methods: {
    // 切换展示的数据源
    changeTableData (item) {
      if (this.currentTabPath === item.pathValue) {
        return
      }
      this.currentTabPath = item.pathValue
      this.getAllData()
    },
    // 获取元数据表的数据
    getAllData () {
      if (!this.currentTabPath) {
        this.$message({
          message: '请选择点击表格上方的按钮, 获取需要展示的数据源',
          type: 'warning'
        })
        return
      }
      this.loading = true
      this.$get(this.currentTabPath).then((data) => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.tableData = dataResult.data
        } else {
          this.$message({
            message: dataResult.msg,
            type: 'warning'
          })
        }
        this.loading = false
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        })
        this.loading = false
      })
    },
    // 运行脚本时
    tryRunMethod (item) {
      if (!item) {
        this.$message({
          message: '没有获取到该脚本的名称',
          type: 'warning'
        })
        return
      }
      this.loading = true
      this.$get('/hj/compiled/content', {
        name: item
      }).then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success' && dataResult.data.content) {
          this.$post('/hj/tryrun', {
            content: dataResult.data.content
          }).then(data => {
            let dataResult = data.data.result
            if (dataResult.code === 'success') {
              this.resultDialog.showDialog = true
              this.resultDialog.resultValue = encodeURIComponent(JSON.stringify(dataResult.data.run_result))
            } else {
              this.$message({
                message: dataResult.msg,
                type: 'warning'
              })
            }
            this.loading = false
          }).catch((error) => {
            this.$message({
              message: error,
              type: 'error'
            })
          })
        } else {
          this.$message({
            message: '没有获取到脚本内容',
            type: 'warning'
          })
          this.loading = false
        }
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        })
        this.loading = false
      })
    },
    // 编译指定的hostjs脚本代码
    compileMethod (item) {
      if (!item) {
        this.$message({
          message: '没有获取到该脚本的名称',
          type: 'warning'
        })
        return
      }
      this.loading = true
      this.$post('/hj/compile', {
        name: item
      }).then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.$message({
            message: '编译成功!',
            type: 'success'
          })
        } else {
          this.$message({
            message: dataResult.msg,
            type: 'warning'
          })
        }
        this.loading = false
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    // 编译所有的hostjs脚本代码
    compileAll () {
      this.loading = true
      this.$post('/hj/compileall').then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.$message({
            message: '编译成功!',
            type: 'success'
          })
        } else {
          this.$message({
            message: dataResult.msg,
            type: 'warning'
          })
        }
        this.loading = false
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    // 新增时
    handleAdd () {
      this.dialogFormVisible_add = true
      this.ruleForm.addName = ''
    },
    // 点击下一步时
    submitForm (forms) {
      this.$refs[forms].validate((valid) => {
        if (valid) {
          this.dialogFormVisible_add = false
          this.editDialog.hostName = this.ruleForm.addName
          this.editDialog.showCompiled = this.currentTabPath === '/hj/source/list'
          this.editDialog.showDialog = true
          this.editDialog.resultValue = null
        } else {
          return false
        }
      })
    },
    // 关闭编辑的弹窗时
    closeEditDialog () {
      if (this.currentTabPath === '/hj/source/list') {
        this.getAllData()
      }
      this.editDialog.showDialog = false
    },
    // 编辑已经编译过的代码
    handleCompiledEdit (item) {
      if (!item) {
        this.$message({
          message: '没有获取到该脚本的名称',
          type: 'warning'
        })
        return
      }
      this.loading = true
      this.editDialog.hostName = item
      this.editDialog.showCompiled = true
      this.$get('/hj/compiled/content', {
        name: item
      }).then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.editDialog.showDialog = true
          this.editDialog.resultValue = dataResult.data.content ? encodeURIComponent(dataResult.data.content) : null
        } else {
          this.$message({
            message: dataResult.msg,
            type: 'warning'
          })
        }
        this.loading = false
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        })
        this.loading = false
      })
    },
    // 编辑时---公用部分
    handleCommlibEdit (item) {
      if (!item) {
        this.$message({
          message: '没有获取到该脚本的名称',
          type: 'warning'
        })
        return
      }
      this.loading = true
      this.editDialog.hostName = item
      this.editDialog.showCompiled = false
      this.$get('/hj/commlib/content', {
        name: item
      }).then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.editDialog.showDialog = true
          this.editDialog.resultValue = dataResult.data.content ? encodeURIComponent(dataResult.data.content) : null
        } else {
          this.$message({
            message: dataResult.msg,
            type: 'warning'
          })
        }
        this.loading = false
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        })
        this.loading = false
      })
    },
    // 编辑时---源码
    handleEdit (item) {
      if (!item) {
        this.$message({
          message: '没有获取到该脚本的名称',
          type: 'warning'
        })
        return
      }
      this.loading = true
      this.editDialog.hostName = item
      this.editDialog.showCompiled = true
      this.$get('/hj/source/content', { // 获取指定的源码的内容
        name: item
      }).then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.editDialog.showDialog = true
          this.editDialog.resultValue = dataResult.data.content ? encodeURIComponent(dataResult.data.content) : null
        } else {
          this.$message({
            message: dataResult.msg,
            type: 'warning'
          })
        }
        this.loading = false
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        })
        this.loading = false
      })
    }
  }
}
